<!--
Copyright 2014 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!DOCTYPE html><meta charset="UTF-8">
<script src="../bootstrap.js"></script>
<script>
'use strict';

function createPlayer() {
  return document.timeline.play(new Animation(null, null, 200));
}

function assertFinishEvents(description, player, expectedEvents) {
  var i = 0;

  expectedEvents.forEach(function (expectedEvent, expectedIndex) {
    var asyncTestHandle = async_test(description + ', t=' + expectedEvent.timelineTime);
    // Testharness quirk: async_test won't time out if step is never called.
    at(expectedEvent.timelineTime, function() {asyncTestHandle.step(function() {});});
    player.addEventListener('finish', function(event) {
      if (i === expectedIndex) {
        asyncTestHandle.step(function() {
          assert_equals(event.currentTime, expectedEvent.currentTime, 'event.currentTime');
          assert_equals(event.timelineTime, expectedEvent.timelineTime, 'event.timelineTime');
          assert_equals(event.target, player, 'event.target');
          assert_approx_equals(document.timeline.currentTime, expectedEvent.timelineTime, 50, 'timeline.currentTime');
        });
        asyncTestHandle.done();
      }
    });
  });

  player.addEventListener('finish', function(event) {
    if (i >= expectedEvents.length) {
      test(function() {assert_true(false);}, 'More than ' + expectedEvents.length + ' events fired for test "' + description + '"');
    }
    i++;
  });
}

var player;

player = createPlayer();
assertFinishEvents('Players should fire finished events when they complete', player, [
  {timelineTime: 200, currentTime: 200},
]);

player = createPlayer();
player.source = null;
assertFinishEvents('A player with no source should report finished on the next sample', player, [
  {timelineTime: 0, currentTime: 0},
]);

player = createPlayer();
player.currentTime = 200;
assertFinishEvents('Jumping to the end of a player\'s animation should fire the finish event', player, [
  {timelineTime: 0, currentTime: 200},
]);

player = createPlayer();
player.currentTime = 200;
player.currentTime = 1;
player.currentTime = 200;
assertFinishEvents('Jumping to the end of a player\'s animation twice should fire the finish event once', player, [
  {timelineTime: 0, currentTime: 200},
]);

var jumpingPlayer = createPlayer();
jumpingPlayer.currentTime = 200;
at(100, function() {jumpingPlayer.currentTime = 100;});
at(200, function() {jumpingPlayer.currentTime = 200;});
assertFinishEvents('Jumping to the end of a player\'s animation twice on different frames should fire the finish event twice', jumpingPlayer, [
  {timelineTime: 0, currentTime: 200},
  {timelineTime: 200, currentTime: 200},
]);

var reversedPlayer = createPlayer();
reversedPlayer.currentTime = 100;
reversedPlayer.reverse();
assertFinishEvents('Reversed players should fire finish events when they reach the start of their animation', reversedPlayer, [
  {timelineTime: 100, currentTime: 0},
]);

// Force the test harness to sample periodically.
for (var t = 0; t < 500; t += 100) {
  at(t, function() {});
}
</script>
